﻿Public Class factura

    Dim cadena As String = "Data Source=CESAR-PC\SQLSERVER;Initial Catalog=CAPCrepuestos;Integrated Security=True"
    Private Sub cargarCombo(ByVal combo As ComboBox, ByRef datos As DataTable, ByVal pk As String, ByVal descripcion As String)

        combo.DataSource = datos
        combo.DisplayMember = descripcion
        combo.ValueMember = pk

    End Sub

    Private Function leo_tabla(ByVal nombreTabla As String) As Data.DataTable

        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM " & nombreTabla
        tabla.Load(cmd.ExecuteReader())

        conexion.Close()

        Return tabla

    End Function
    
    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click
        ventas.ShowDialog()

    End Sub

    Private Sub factura_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cargarCombo(cmbTipoFactura, leo_tabla("tipo_factura"), "id", "descripcion")
        txtDNI.Enabled = False
        txtFecha.Enabled = False
        txtNombre.Enabled = False
        txtNroFactura.Enabled = False
        txtRazonSocial.Enabled = False
        txtTotal.Enabled = False
        cmbTipoFactura.Enabled = False
        txtCUIT.Enabled = False
    End Sub

    Private Sub txtNroFactura_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroFactura.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtNroFactura.MaxLength = 6
    End Sub

    Private Sub txtDNI_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDNI.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtDNI.MaxLength = 8
    End Sub

    Private Sub txtFecha_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFecha.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
    End Sub

    Private Sub txtNombre_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNombre.KeyPress
        If Not Char.IsLetter(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 Then e.Handled = True
        txtNombre.MaxLength = 100
    End Sub

    Private Sub txtRazonSocial_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtRazonSocial.KeyPress
        If Not Char.IsLetter(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 32 Then e.Handled = True
        txtRazonSocial.MaxLength = 100
    End Sub

    Private Sub txtTotal_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTotal.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 46 Then e.Handled = True
        txtTotal.MaxLength = 20
    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        txtDNI.Enabled = True
        txtFecha.Enabled = True
        txtFecha.Text = Today
        txtNombre.Enabled = True
        txtRazonSocial.Enabled = True
        txtTotal.Enabled = True
        txtCUIT.Enabled = True
        cmbTipoFactura.Enabled = True

        txtNroFactura.Text = cargarMayor()

    End Sub

    Private Function cargarMayor() As Integer
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim max As Integer

        conexion.ConnectionString = cadena
        conexion.Open()

        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT MAX(id) as id FROM factura"
        max = cmd.ExecuteScalar
        conexion.Close()

        Return max + 1
    End Function
End Class